<?php
class SLCPicture {
    var $pictureName = null;
    var $picturePath = null;

    var $dbPicName = null;
    var $path = null;
    var $name = null;

    var $thumbWidth = null;

    function slc_uploadFile($controlName, $picName, $picPath) {
        global $pictureName, $name, $picturePath, $path;

        $pictureName = $controlName;
        $name = $picName;
        $picturePath = $picPath;


        $file = $_FILES[$pictureName]['tmp_name'];

        if($name == '')
                $newFile = str_replace(" ", "_", $_FILES[$pictureName]['name']);
        else
                $newFile = str_replace(" ", "_", $name) .'_'. str_replace(" ", "_", $_FILES[$pictureName]['name']);

        $fpath = $picturePath . $newFile;
        move_uploaded_file($file, $fpath);

        $path = $fpath;
        $dbPicName = $newFile;
    }

    function slc_createThumb($thumbWidth) {
        global $picturePath, $path, $pictureName, $name, $thumbName;

        $pictureType = $_FILES[$pictureName]['type'];

        if($pictureType == 'image/jpeg') {
            $thumb = imagecreatefromjpeg($path);
            $width = imagesx($thumb);
            $height = imagesy($thumb);

            $thumbW = $thumbWidth;
            $thumbH = floor($height * ($thumbW / $width));

            $thumb_tmp = imagecreatetruecolor($thumbW, $thumbH);
            imagecopyresized($thumb_tmp, $thumb, 0, 0, 0, 0, $thumbW, $thumbH, $width, $height);
            if($name == '')
                    $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $_FILES[$pictureName]['name']);
            else
                    $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $name) . '_' . str_replace(" ", "_", $_FILES[$pictureName]['name']);
            imagejpeg($thumb_tmp, $thumb_name);
        }
        elseif($pictureType == 'image/gif') {
            $thumb = imagecreatefromgif($path);
            $width = imagesx($thumb);
            $height = imagesy($thumb);

            $thumbW = $thumbWidth;
            $thumbH = floor($height * ($thumbW / $width));

            $thumb_tmp = imagecreatetruecolor($thumbW, $thumbH);
            imagecopyresized($thumb_tmp, $thumb, 0, 0, 0, 0, $thumbW, $thumbH, $width, $height);
            if($name == '')
                    $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $_FILES[$pictureName]['name']);
            else
                    $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $name) . '_' . str_replace(" ", "_", $_FILES[$pictureName]['name']);
            imagejpeg($thumb_tmp, $thumb_name);
        }
        elseif($pictureType == 'image/png') {
            $thumb = imagecreatefrompng($path);
            $width = imagesx($thumb);
            $height = imagesy($thumb);

            $thumbW = $thumbWidth;
            $thumbH = floor($height * ($thumbW / $width));

            $thumb_tmp = imagecreatetruecolor($thumbW, $thumbH);
            imagecopyresized($thumb_tmp, $thumb, 0, 0, 0, 0, $thumbW, $thumbH, $width, $height);
            if($name == '')
                    $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $_FILES[$pictureName]['name']);
            else
                    $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $name) . '_' . str_replace(" ", "_", $_FILES[$pictureName]['name']);
            imagejpeg($thumb_tmp, $thumb_name);
        }
    }

    function slc_thumbTest($thumbWidth, $numepoza) {
        global $picturePath, $path, $pictureName, $name, $thumbName;

        $pictureType = $_FILES[$pictureName]['type'];

        if($pictureType == 'image/jpeg') {
            $thumb = imagecreatefromjpeg($path);
            $width = imagesx($thumb);
            $height = imagesy($thumb);

            $thumbW = $thumbWidth;
            $thumbH = floor($height * ($thumbW / $width));

            $thumb_tmp = imagecreatetruecolor($thumbW, $thumbH);
            imagecopyresized($thumb_tmp, $thumb, 0, 0, 0, 0, $thumbW, $thumbH, $width, $height);
            if($name == '')
                $thumb_name = $picturePath . "thumbs/thumb_".$numepoza.'-'. str_replace(" ", "_", $_FILES[$pictureName]['name']);
            else
                $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $name) . '_'.$numepoza.'-'. str_replace(" ", "_", $_FILES[$pictureName]['name']);
            imagejpeg($thumb_tmp, $thumb_name);
        }
        elseif($pictureType == 'image/gif') {
            $thumb = imagecreatefromgif($path);
            $width = imagesx($thumb);
            $height = imagesy($thumb);

            $thumbW = $thumbWidth;
            $thumbH = floor($height * ($thumbW / $width));

            $thumb_tmp = imagecreatetruecolor($thumbW, $thumbH);
            imagecopyresized($thumb_tmp, $thumb, 0, 0, 0, 0, $thumbW, $thumbH, $width, $height);
            if($name == '')
                $thumb_name = $picturePath . "thumbs/thumb_".$numepoza.'-'. str_replace(" ", "_", $_FILES[$pictureName]['name']);
            else
                $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $name) . '_'.$numepoza.'-'. str_replace(" ", "_", $_FILES[$pictureName]['name']);
            imagejpeg($thumb_tmp, $thumb_name);
        }
        elseif($pictureType == 'image/png') {
            $thumb = imagecreatefrompng($path);
            $width = imagesx($thumb);
            $height = imagesy($thumb);

            $thumbW = $thumbWidth;
            $thumbH = floor($height * ($thumbW / $width));

            $thumb_tmp = imagecreatetruecolor($thumbW, $thumbH);
            imagecopyresized($thumb_tmp, $thumb, 0, 0, 0, 0, $thumbW, $thumbH, $width, $height);
            if($name == '')
                $thumb_name = $picturePath . "thumbs/thumb_".$numepoza.'-'. str_replace(" ", "_", $_FILES[$pictureName]['name']);
            else
                $thumb_name = $picturePath . "thumbs/thumb_" . str_replace(" ", "_", $name) . '_'.$numepoza.'-'. str_replace(" ", "_", $_FILES[$pictureName]['name']);
            imagejpeg($thumb_tmp, $thumb_name);
        }
    }

    function slc_deleteFile($picPath, $picName) {
        if(file_exists($picPath . $picName))
            unlink($picPath . $picName);
        if(file_exists($picPath . "thumbs/thumb_" . $picName))
            unlink($picPath . "thumbs/thumb_" . $picName);
    }
}
?>